package org.gluedom.data;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.gluedom.data.Immutables.checkImmutable;

import java.util.Collection;
import java.util.List;

import com.google.common.collect.ImmutableList;

public class AttributeValue implements Immutable, Ownable, PropertyContainer {
	private final Identifier ownerId;
	private final Identifier conceptId;
	private final Identifier attributeId;
	private final Object value;
	private final List<Property> properties;

	private volatile int hash = 0;

	private AttributeValue(Builder builder) {
		ownerId = builder.ownerId.ownerId;
		conceptId = builder.conceptId;
		attributeId = builder.attributeId;
		value = builder.value;
		properties = ImmutableList.copyOf(builder.properties.properties);
	}

	@Override
	public Identifier getOwnerId() {
		return ownerId;
	}

	public Identifier getConceptId() {
		return conceptId;
	}

	public Identifier getAttributeId() {
		return attributeId;
	}

	public Object getValue() {
		return value;
	}

	@Override
	public Iterable<Property> getProperties() {
		return properties;
	}

	@Override
	public String getProperty(String name) {
		return Property.findProperty(name, properties);
	}

	public static class Builder {
		private final Ownable.Builder ownerId;
		private Identifier conceptId;
		private Identifier attributeId;
		private Object value;
		private final PropertyContainer.Builder properties = new PropertyContainer.Builder();

		public Builder(AttributeValue attributeValue) {
			ownerId = new Ownable.Builder(attributeValue.ownerId);
			conceptId = attributeValue.conceptId;
			attributeId = attributeValue.attributeId;
			value = attributeValue.value;
			properties.properties(attributeValue.properties);
		}

		public Builder(Identifier ownerId, Identifier conceptId,
				Identifier attributeId, Object value) {
			this.ownerId = new Ownable.Builder(checkNotNull(ownerId));
			this.conceptId = checkNotNull(conceptId);
			this.attributeId = checkNotNull(attributeId);
			this.value = checkImmutable(checkNotNull(value));
		}

		public Builder ownerId(Identifier ownerId) {
			this.ownerId.ownerId(checkNotNull(ownerId));
			return this;
		}

		public Builder conceptId(Identifier conceptId) {
			this.conceptId = checkNotNull(conceptId);
			return this;
		}

		public Builder attributeId(Identifier attributeId) {
			this.attributeId = checkNotNull(attributeId);
			return this;
		}

		public Builder value(Object value) {
			this.value = checkImmutable(checkNotNull(value));
			return this;
		}

		public Builder property(String name, String value) {
			properties.property(name, value);
			return this;
		}

		public Builder property(Property property) {
			properties.property(checkNotNull(property));
			return this;
		}

		public Builder properties(Collection<Property> properties) {
			this.properties.properties(properties);
			return this;
		}

		public AttributeValue build() {
			return new AttributeValue(this);
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof AttributeValue))
			return false;
		AttributeValue other = (AttributeValue) obj;
		if (hashCode() != other.hashCode())
			return false;
		if (!ownerId.equals(other.ownerId))
			return false;
		if (!conceptId.equals(other.conceptId))
			return false;
		if (!attributeId.equals(other.attributeId))
			return false;
		if (!value.equals(other.value))
			return false;
		if (!properties.equals(other.properties))
			return false;
		return true;
	}

	@Override
	public int hashCode() {
		if (hash == 0) {
			int result = 105873;
			result = 31 * result + ownerId.hashCode();
			result = 31 * result + conceptId.hashCode();
			result = 31 * result + attributeId.hashCode();
			result = 31 * result + value.hashCode();
			result = 31 * result + properties.hashCode();
			hash = result;
		}
		return hash;
	}

	@Override
	public String toString() {
		return conceptId.resolve(attributeId).toString() + " `" + value
				+ "` $owner `" + ownerId + "` " + properties;
	}
}
